import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import '../../../../base/base_state.dart';
import '../../../../http/api.dart';
import '../../../../http/http_utils.dart';
import '../../../../res/w_dimens.dart';
import '../../../../utils/common_utils.dart';
import '../../../../utils/router_helper.dart';
import '../../../../utils/string_utils.dart';
import '../../../../widget/label_item.dart';
import '../../../../widget/w_text.dart';
import '../../../../widget/w_toast.dart';

class DriveCancelPage extends StatefulWidget {
  int testDriveId;

  DriveCancelPage({super.key, required this.testDriveId});

  @override
  BaseState<DriveCancelPage> createState() => _DriveCancelPageState();
}

class _DriveCancelPageState extends BaseState<DriveCancelPage> {
  final _formKey = GlobalKey<FormState>();

  //初始化试驾预约登记对象
  Map driveInfo = {
    "id": "",
    "remark": "",
  };

  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
        title: "取消预约",
        resizeToBottomInset: true,
        child: Column(children: [
          Expanded(
            child: SingleChildScrollView(
                padding: EdgeInsets.zero, child: _bookDriveItem()),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 10.px, vertical: 10.px),
            child: GestureDetector(
              onTap: () {
                CommonUtils.throttle(cancel);
              },
              child: Container(
                margin:
                    EdgeInsets.symmetric(vertical: 16.px, horizontal: 10.px),
                width: SizeUtils.screenW() - 48.px,
                alignment: Alignment.center,
                height: 48.px,
                decoration: BoxDecoration(
                  color: WColors.primaryColor,
                  borderRadius: BorderRadius.circular(24.px),
                ),
                child: WText(
                  "保存",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: WDimens.tsLarge,
                  ),
                ),
              ),
            ),
          ),
        ]));
  }

  void cancel() {
    driveInfo["id"] = widget.testDriveId;
    showLoading();
    HttpUtils()
        .post(Api.saveTestDriveRegistrationInfo, data: driveInfo)
        .then((value) {
      hideLoading();
      if (value.resultCode == 200) {
        Toast.toast("取消成功");
        RouteHelper.maybePop(context, true);
      } else {
        Toast.toast("取消失败");
      }
    }).catchError((e) {
      hideLoading();
    });
  }

  Widget _bookDriveItem() {
    return Form(
        key: _formKey,
        child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(left: 16.px, right: 16.px, bottom: 10.px),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.px),
                color: Colors.white,
                boxShadow: const [
                  BoxShadow(
                      //线框阴影线条
                      color: WColors.border,
                      offset: Offset(0, 1),
                      blurRadius: 2)
                ]),
            child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  SizedBox(
                    height: 1.px,
                  ),
                  LabelItem(
                    labelType: LabelType.TEXTAREA,
                    label: "取消原因",
                    value: driveInfo["remark"],
                    maxTextLength: 500,
                    onChanged: (text) {
                      setState(() {
                        driveInfo["remark"] = text;
                      });
                    },
                  ),
                ])));
  }


}
